<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Anquanyuan;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\Renderable\DeptTable;
use App\Models\Dept;

class AnquanyuanController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Anquanyuan(), function (Grid $grid) {

            $grid->quickSearch('aqyname', 'dept.deptname')->placeholder('输入部门或姓名搜索...');
            // 禁用过滤器按钮
            $grid->disableFilterButton();
            // 禁用刷新按钮
            $grid->disableRefreshButton();
            // 禁用行选择器
            $grid->disableRowSelector();

            $grid->column('id')->sortable();
            $grid->model()->orderBy('id', 'desc');

            $grid->model()->with(['dept']);
            $grid->column('id')->sortable();
            $grid->column('aqyname');
            $grid->column('aqygonghao');
            $grid->column('aqytel');
            $grid->column('dept.deptname', '部门名称');
            // $grid->column('created_at');
            // $grid->column('updated_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Anquanyuan(), function (Show $show) {
            $show->field('id');
            $show->field('aqyname');
            $show->field('aqygonghao');
            $show->field('aqytel');
            $show->field('dept_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Anquanyuan(), function (Form $form) {
            $form->display('id');
            $form->text('aqyname')->required();
            $form->text('aqygonghao')->required();
            $form->text('aqytel')->required();
            // $form->text('dept_id');
            $form->selectTable('dept_id', '所属部门：')
                ->title('部门列表')
                ->from(DeptTable::make())
                ->model(Dept::class, 'id', 'deptname')->required();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
